<?php

namespace App\Http\Controllers;

use App\Models\Integrallist;
use App\Models\Property;
use App\Models\User;
use App\Traits\ValidationTrait;
use Exception;
use Illuminate\Http\Request;

/**
 * 注册
 */
class RegisterController extends Controller
{
    use ValidationTrait;
    /**
     * 注册账号
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function register_account(Request $request)
    {
        try {
            $status = 0;
            $data   = [];
            //验证
            $validator = $this->verify($request, 'validation.register_account');
            if ($validator->fails()) {
                $messages = $validator->messages()->first();

                throw new Exception($messages);
            }

            $phone    = $request->get('phone');
            $code     = $request->get('code');
            $password = $request->get('password');
            $repass   = $request->get('repass');

            if ($password != $repass) {
                throw new Exception('两次密码不一致！');
            }

            /*$sms = Sms::where(['phone' => $phone, 'type' => 3])->first();
            //验证码过期
            if (time() > $sms->expired_at) {
            throw new Exception('验证码已过期！');
            }
            //验证码错误
            if ($sms->code != $code) {
            //if ($code != '666666') {
            throw new Exception('验证码错误！');
            }*/

            //判断手机号是否注册
            $user = User::where(['phone' => $phone, 'type' => 4])->first();
            if ($user) {
                throw new Exception('手机号已注册');
            }

            //写入数据库
            $result = User::create([
                'phone'         => $phone,
                'password'      => md5($password . config('public.password_key')),
                'PID'           => 0,
                'register_time' => time(),
                'last_time'     => time(),
                'integral'      => 50, //注册成功送50积分
                'account'       => getAccount(),
                'nickname'      => getNickname(),
                'sign'          => getSign(),
                'token1'        => getToken(),
                'type'          => 4, //1为创客 3为推荐vip 4无推荐
                'headimg_oss'   => randomHeadimg(),
                'sole'          => '4' . $phone,
            ]);

            //生成一个资产余额
            Property::create([
                'userID'      => $result->userID,
                'name'        => '余额',
                'type'        => 1,
                'value'       => 0,
                'grand_total' => 0,
                'status'      => 1,
                'time'        => time(),
            ]);

            //写入积分表
            Integrallist::create([
                'userID'   => $result->userID,
                'integral' => 50,
                'type'     => 1,
                'property' => 1,
                'id'       => $result->userID,
                'note'     => '您已获得注册的50积分。',
                'time'     => time(),
            ]);

            $status           = 1;
            $msg              = '注册成功！';
            $data['user_id']  = $result->userID;
            $data['nickname'] = $result->nickname;
            $data['account']  = $result->account;
            $data['headimg']  = $result->headimg;

        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }
}
